/**
* 版权声明：厦门睿商网络科技有限公司 版权所有 违者必究
* 日    期：2020-03-23
*/
package com.rzico.article.controller;

import cn.hutool.core.util.RandomUtil;
import com.alipay.api.domain.StallEntity;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.rzico.annotation.Log;
import com.rzico.article.entity.ArticleLaud;
import com.rzico.article.entity.Talent;
import com.rzico.article.entity.TalentFollow;
import com.rzico.article.model.TalentVo;
import com.rzico.article.service.ArticleLaudService;
import com.rzico.article.service.TalentFollowService;
import com.rzico.article.service.TalentService;
import com.rzico.base.BaseController;
import com.rzico.base.CommResult;
import com.rzico.basics.entity.Enterprise;
import com.rzico.basics.entity.Member;
import com.rzico.basics.service.EnterpriseService;
import com.rzico.basics.service.MemberService;
import com.rzico.entity.Filter;
import com.rzico.entity.Pageable;
import com.rzico.entity.PageResult;
import com.rzico.article.entity.Article;
import com.rzico.article.service.ArticleService;
import com.rzico.exception.CustomException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.Api;
import com.rzico.util.StringUtils;

import java.util.*;
import java.util.function.Consumer;

/**
 * 文章管理控制层
 *
 * @author Rzico Boot
 * @version 1.0
 * @date 2020-03-23
 */
@Api(description = "文章管理接口")
@RestController
@RequestMapping("/article")
public class ArticleController extends BaseController {

    @Autowired
    private ArticleService articleService;
    @Autowired
    private ArticleLaudService articleLaudService;
    @Autowired
    private EnterpriseService enterpriseService;
    @Autowired
    private MemberService memberService;
    @Autowired
    private TalentService talentService;
    @Autowired
    private TalentFollowService talentFollowService;
    /**
     * 分页查询文章管理
     *
     * @return
     */
    @ApiOperation("分页查询文章管理")
    @GetMapping("/list")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "mchId", value = "商户号", dataType = "String", paramType = "query") ,
            @ApiImplicitParam(name = "title", value = "标题", dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "keyword", value = "标题", dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "tagIds", value = "标签", dataType = "Long", paramType = "query"),
            @ApiImplicitParam(name = "articleCategoryId", value = "分类 id", dataType = "Long", paramType = "query"),
            @ApiImplicitParam(name = "talentId", value = "达人id", dataType = "Long", paramType = "query"),
            @ApiImplicitParam(name = "articleType", value = "文章类型 {0:文章,1:热点,2:贴子,3.报名}", dataType = "Long", paramType = "query")
    })
    public CommResult<Article> list(String title,String keyword,String mchId,Integer articleType,Integer articleCategoryId,Long [] tagIds,Long talentId, Pageable pageable) {
        Map<String, Object> params = new HashMap<String, Object>();
        params = buildSortField(params, pageable);


        Enterprise enterprise = enterpriseService.findByMch(mchId);
        if (enterprise!=null) {
            params.put("enterpriseId", enterprise.getId());
        } else {
            return CommResult.error("没有开通企业");
        }

        if (tagIds!=null) {
            params.put("tagIds",tagIds);
        }


        if (articleType!=null) {
            params.put("articleType",articleType);
        } else {
            params.put("articleType",0);
        }


        params.put("deleted", false);
        params.put("isDraft", false);

        if (title!=null) {
            params.put("title", title);
        }

        if (articleType==null) {
            articleType = 0;
        }
        params.put("articleType", articleType);

        if (articleCategoryId!=null) {
           params.put("treePath", String.valueOf(articleCategoryId));
        }

        //如果是达人详情列表的话,传入talentId
        if (talentId!=null) {
            params.put("talentId", talentId);
        }

        if (keyword!=null) {
            params.put("keyword", keyword);
        }

        Page<Object> startPage = PageHelper.startPage(pageable.getPageNum(), pageable.getPageSize());
        List<Article> list = articleService.selectList(params);
        PageResult<Article> pageResult = new PageResult<Article>(list, startPage.getTotal(), pageable);

        Member member = memberService.getCurrent();
        //对查出来的article进行处理
        if (params.containsKey("talentId") && member != null) {
            articleService.laudDispose(params,member,pageResult.getData());
        }
        return CommResult.success(pageResult);
    }

    /**
     * 查询单条文章管理
     *
     * @return
     */
    @ApiOperation("查询单条文章管理")
    @GetMapping("/find/{id}")
    public CommResult<Article> find(@PathVariable String id) {
        Article result = articleService.findById(id);
        if (result == null) {
            throw new CustomException("此文章不存在");
        }
        articleService.incHits(result);
        Map<String,Object> data = new HashMap<>();
        data.put("article",result);
        if (result.getTalentId() == null || result.getTalentId() <= 0) {
            return CommResult.success(data);
        }
        TalentVo  talentVo = talentService.findTalentVoByTalentId(result.getTalentId());
        if (talentVo == null) {
            return CommResult.success(data);
        }
        talentVo.setFollowFlag(false);
        data.put("talent",talentVo);
        Member member = memberService.getCurrent();
        if (member == null) {
            return CommResult.success(data);
        }
        Map<String, Object> params = new HashMap<>();
        params.put("memberId", member.getId());
        params.put("talentId",talentVo.getId());
        List<TalentFollow> talentFollows = talentFollowService.selectList(params);
        if (talentFollows != null && talentFollows.size() == 1) {
            talentVo.setFollowFlag(true);
        }
        return CommResult.success(data);
    }



    /**
     * 获取报名的友情链接
     *
     * @return
     */
    @ApiOperation("获取报名的友情链接")
    @GetMapping("/friend_link")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "mainProductId", value = "主商品ID", dataType = "Long", paramType = "query")
    })
    public CommResult<Article> friend_link(Long mainProductId, Pageable pageable) {
        Map<String, Object> params = new HashMap<String, Object>();
        params = buildSortField(params, pageable);

        if (mainProductId!=null) {
            params.put("friendLinkId", mainProductId);
        }

        Page<Object> startPage = PageHelper.startPage(pageable.getPageNum(), pageable.getPageSize());
        List<Article> list = articleService.selectList(params);
        PageResult<Article> pageResult = new PageResult<Article>(list, startPage.getTotal(), pageable);
        return CommResult.success(pageResult);
    }

}
